package com.yst.gateway.monitor.controllers;

import java.util.Map;
import javax.validation.groups.Default;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.common.validator.groups.Groups;
import com.yst.gateway.monitor.models.SysMenus;
import com.yst.gateway.monitor.services.SysMenuService;

/**
 * 前置系统系统菜单管理访问控制类
 * @author sheungxin
 *
 */
@RestController
@RequestMapping(value = "/sys/menus")
public class SysMenuController {
	
	@Autowired
	private SysMenuService sysMenuService;
	
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public Response post(@RequestBody @Validated({Default.class,Groups.Add.class}) SysMenus sysMenu){
		Response response=new Response();
		if(sysMenuService.add(sysMenu)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/modify",method = RequestMethod.PUT)
	public Response put(@RequestBody @Validated({Default.class,Groups.Modify.class}) SysMenus sysMenu){
		Response response=new Response();
		if(sysMenuService.update(sysMenu)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/lock/{id}",method = RequestMethod.PUT)
	public Response lock(@PathVariable("id") String id){
		Response response=new Response();
		if(sysMenuService.updateStatusById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
	
	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Response get(@PathVariable("id") String id) {
		Response response=new Response();
		SysMenus menu=sysMenuService.getById(id);
		if(menu!=null){
			response.success(menu);
		}else{
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public Response getAll(@RequestParam Map<String, Object> paramMap) {
		Response response=new Response();
		response.success(sysMenuService.getByMap(paramMap));
		return response;
	}
	
	@RequestMapping(value = "/del/{id}",method = RequestMethod.PUT)
	public Response del(@PathVariable("id") String id){
		Response response=new Response();
		if(sysMenuService.delById(id)){
			response.success();
		}else{
			response.failure();
		}
		return response;
	}
}
